<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="方案名称" prop="schemePlan">
        <el-input
          v-model="queryParams.schemeName"
          placeholder="请输入方案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员名字" prop="memName">
        <el-input
          v-model="queryParams.memName"
          placeholder="请输入会员名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="会员生日" prop="memBirth">
        <el-date-picker clearable
          v-model="queryParams.memBirth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择会员生日">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="会员手机" prop="memMobile">
        <el-input
          v-model="queryParams.memMobile"
          placeholder="请输入会员手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="会员电话" prop="memTel">
        <el-input
          v-model="queryParams.memTel"
          placeholder="请输入会员电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="身份证号" prop="memIdentify">
        <el-input
          v-model="queryParams.memIdentify"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡片状态" prop="cardState">
        <el-select v-model="queryParams.cardState" placeholder="请选择卡片状态">
          <el-option v-for="dict in dict.type.sys_card_state" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="有效期" prop="limitDate">
        <el-date-picker clearable
          v-model="queryParams.limitDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="activationCard"
          v-hasPermi="['system:info:edit']"
        >激活</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="卡号" align="center" prop="cardNo" />
<!--      <el-table-column label="实体介质卡内部序列号" align="center" prop="cardSbh" />-->
<!--      <el-table-column label="卡片类型" align="center" prop="cardType" />-->
      <el-table-column label="卡片类型" align="center" prop="cardType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_card_type" :value="scope.row.cardType"/>
        </template>
      </el-table-column>
      <el-table-column label="销售方案名称" align="center" prop="schemeName" />
      <el-table-column label="余额" align="center" prop="balance" />
      <el-table-column label="积分" align="center" prop="score" />
      <el-table-column label="折扣率" align="center" prop="discountRate" />
      <el-table-column label="次数" align="center" prop="times" />
<!--      <el-table-column label="卡唯一标识" align="center" prop="cardGuid" />-->
      <el-table-column label="会员名字" align="center" prop="memName" />
<!--      <el-table-column label="会员生日" align="center" prop="memBirth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="会员手机" align="center" prop="memMobile" />
<!--      <el-table-column label="会员电话" align="center" prop="memTel" />
      <el-table-column label="身份证号" align="center" prop="memIdentify" />
      <el-table-column label="会员地址" align="center" prop="memAddr" />-->
      <el-table-column label="卡片状态" align="center" prop="cardState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_card_state" :value="scope.row.cardState"/>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="limitDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.limitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="memo" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="6位安全码" align="center" prop="cvv" />-->

<!--      <el-table-column label="支付密码" align="center" prop="pwd" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
        <el-form-item label="卡号" prop="cardNo">
          <el-input v-model="form.cardNo" readonly placeholder="请输入卡号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入余额" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="积分" prop="score">
          <el-input v-model="form.score" placeholder="请输入积分" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="折扣率" prop="discountRate">
          <el-input v-model="form.discountRate" placeholder="请输入折扣率" />
        </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="次数" prop="times">
          <el-input v-model="form.times" placeholder="请输入次数" />
        </el-form-item>
            </el-col>
          <el-col :span="12">
        <el-form-item label="会员名字" prop="memName">
          <el-input v-model="form.memName" placeholder="请输入会员名字" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="会员生日" prop="memBirth">
          <el-date-picker clearable
            v-model="form.memBirth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择会员生日">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="会员手机" prop="memMobile">
          <el-input v-model="form.memMobile" placeholder="请输入会员手机" />
        </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="身份证号" prop="memIdentify">
          <el-input v-model="form.memIdentify" placeholder="请输入身份证号" />
        </el-form-item>
            </el-col>
          <el-col :span="12">
        <el-form-item label="卡片状态" prop="cardState">
            <el-select v-model="form.cardState" placeholder="请输入卡片状态">
              <el-option v-for="dict in dict.type.sys_card_state" :key="dict.value" :label="dict.label" :value="Number(dict.value)"></el-option>
            </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="有效期" prop="limitDate">
          <el-date-picker clearable
            v-model="form.limitDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="销售方案名称" prop="schemePlan">
          <el-input v-model="form.schemeName" placeholder="销售方案名称" />
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, updateCardType } from "@/api/system/info";

export default {
  name: "Info",
  dicts: ['sys_card_state', 'sys_card_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardNo: null,
        cardSbh: null,
        cardType: null,
        balance: null,
        score: null,
        discountRate: null,
        times: null,
        isbalance: null,
        isscore: null,
        isdiscount: null,
        istimes: null,
        isequity: null,
        cardGuid: null,
        memName: null,
        memBirth: null,
        memMobile: null,
        memTel: null,
        memIdentify: null,
        memAddr: null,
        cardState: null,
        limitDate: null,
        memo: null,
        inputDate: null,
        deptId: null,
        cvv: null,
        schemePlan: null,
        pwd: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cardNo: [
          { required: true, message: "卡号不能为空", trigger: "blur" }
        ],
        cardType: [
          { required: true, message: "卡类型", trigger: "change" }
        ],
        times: [
          { required: true, message: "次数不能为空", trigger: "blur" }
        ],
        memMobile: [
          { required: true, message: "会员手机不能为空", trigger: "blur" }
        ],
        cardState: [
          { required: true, message: "卡片状态", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cardId: null,
        cardNo: null,
        cardSbh: null,
        cardType: null,
        balance: null,
        score: null,
        discountRate: null,
        times: null,
        isbalance: null,
        isscore: null,
        isdiscount: null,
        istimes: null,
        isequity: null,
        cardGuid: null,
        memName: null,
        memBirth: null,
        memMobile: null,
        memTel: null,
        memIdentify: null,
        memAddr: null,
        cardState: null,
        limitDate: null,
        memo: null,
        inputDate: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        cvv: null,
        schemePlan: null,
        pwd: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cardId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cardId = row.cardId || this.ids
      getInfo(cardId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员信息";
      });
    },
    activationCard(row) {
      const cardIds = row.cardId || this.ids;
      if (cardIds) {
        cardIds.forEach(cardId => {
          const card = this.infoList.find(item => item.cardId === cardId)
          console.log(card)
          if (!card.memMobile  || card.memMobile.trim() === "") {
            this.$modal.alertError("请先填写会员手机号码")
            return
          }
        })
      }
      this.handleCardType(cardIds, 1)
    },
    /** 卡片状态操作 */
    handleCardType(cardIds, type) {
      this.$modal.confirm('是否确认激活卡片？').then(function() {
        return updateCardType(cardIds, type);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cardId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cardIds = row.cardId || this.ids;
      this.$modal.confirm('是否确认删除会员信息编号为"' + cardIds + '"的数据项？').then(function() {
        return delInfo(cardIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
